package com.next.aappublicapp.server.service;

import java.util.Arrays;
import java.util.List;

import org.springframework.http.ContentCodingType;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import android.content.Context;
import android.util.Log;

import com.facebook.Session;
import com.next.aap.client.AllItem;
import com.next.aap.client.AssemblyConstituencyDto;
import com.next.aap.client.AssemblyConstituencyWithCandidate;
import com.next.aap.client.AssemblyConstituencyWithManifesto;
import com.next.aap.client.CandidateDto;
import com.next.aap.client.CandidateSelectionProcess;
import com.next.aap.client.DistrictDto;
import com.next.aap.client.DonationSummary;
import com.next.aap.client.ManifestoDto;
import com.next.aap.client.RegisterDeviceResponse;
import com.next.aap.client.RegisterFacebookUserProfile;
import com.next.aap.client.RegisterFacebookUserResponse;
import com.next.aap.client.StateDto;
import com.next.aap.util.DataUtil;
import com.next.aappublicapp.util.LanguageUtil;
import com.next.aappublicapp.util.PreferencesUtil;

public class DataServices {

		//private static final String baseUrl = "http://192.168.1.2:8081/aap/api/";
		//private static final String baseUrl = "http://aappublicserver001snapshot.uhurucloud.com/api/";
		private static final String baseUrl = "http://ec2-54-214-240-26.us-west-2.compute.amazonaws.com:8080/aap/api/";
		
		private static final String getAllItemsUrl = "get/all";
		//private static final String addNewsUrl = "news/add";
		private static final String registerDeviceUrl = "device/register/android";
		private static final String registerFacebookUserUrl = "user/register/facebook";
		private static final String facebookFeedUrl = "facebook/getfeed";
		private static final String englishCandidateSelectionProcessUrl = "csp/get/eng";
		private static final String hindiCandidateSelectionProcessUrl = "csp/get/hin";
		private static final String donationSummaryUrl = "donation/get";
		private static final String getAllStatesUrl = "state/getall";
		private static final String getDistrictOfStateUrl = "districtofstate/get";
		private static final String getAssemblyConstituencyOfDistrictUrl = "acofdistrict/get";
		private static final String getAssemblyConstituencyOfStateUrl = "acofstate/get";
		private static final String getAssemblyConstituencyOfStateWithCandidatesUrl = "acofstate/getforcandidate";
		private static final String getAssemblyConstituencyOfStateWithManifestoUrl = "acofstate/getformanifesto";
		private static final String getAssemblyConstituencyManifestoUrl = "manifesto/get";
		private static final String getAssemblyConstituencyCandidatesUrl = "candidate/get";
		
		
		private static DataServices instance = new DataServices();
		private DataServices(){
			
		}
		public static DataServices getInstance(){
			return instance;
		}
		
		public static <T> T postSpringData(String url, Class<T> responseType,Object message) {
			Log.i("DataServicesImpl","Hitting Post Url "+ url);
			HttpHeaders requestHeaders = new HttpHeaders();
			//requestHeaders.setContentType(new MediaType("application", "json"));
			requestHeaders.setAcceptEncoding(ContentCodingType.GZIP);
			HttpEntity<Object> requestEntity = new HttpEntity<Object>(message,requestHeaders);
			
			RestTemplate restTemplate = new RestTemplate();
			restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

			ResponseEntity<T> responseNtity = restTemplate.postForEntity(url, requestEntity, responseType);
			Log.i("DataServicesImpl","responseNtity.getBody : "+ responseNtity.getBody());
			
			return responseNtity.getBody();
		}
		public static <T> T getSpringData(String url, Class<T> responseType) {
			Log.i("DataServicesImpl","Hitting Get Url "+ url);
			HttpHeaders requestHeaders = new HttpHeaders();
			requestHeaders.setContentType(new MediaType("application", "json"));
			requestHeaders.setAcceptEncoding(ContentCodingType.GZIP);
			RestTemplate restTemplate = new RestTemplate();
			
			restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
			restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
			ResponseEntity<T> responseNtity = restTemplate.getForEntity(url, responseType);
			Log.i("DataServicesImpl","responseNtity.getBody : "+ responseNtity.getBody());
			return responseNtity.getBody();
		}
		
		public CandidateSelectionProcess getEnglishCandidateSelectionProcess(Context context) {
			String url = baseUrl + englishCandidateSelectionProcessUrl;
			CandidateSelectionProcess candidateSelectionProcess = getSpringData(url, CandidateSelectionProcess.class);
			PreferencesUtil.getInstance().saveCandidateSelectionProcessEnglishCache(context, candidateSelectionProcess);
			return candidateSelectionProcess;
		}
		public CandidateSelectionProcess getHindiCandidateSelectionProcess(Context context) {
			String url = baseUrl + hindiCandidateSelectionProcessUrl;
			CandidateSelectionProcess candidateSelectionProcess = getSpringData(url, CandidateSelectionProcess.class);
			PreferencesUtil.getInstance().saveCandidateSelectionProcessHindiCache(context, candidateSelectionProcess);
			return candidateSelectionProcess;
		}
		public AllItem getAllItems(Context context) {
			String url = baseUrl + getAllItemsUrl;
			AllItem allItem = getSpringData(url, AllItem.class);
			PreferencesUtil.getInstance().saveAllItemCache(context, allItem);
			return allItem;
		}
		public RegisterDeviceResponse registerDevice(String deviceId) {
			String url = baseUrl + registerDeviceUrl+"/"+deviceId;
			RegisterDeviceResponse registerDeviceResponse = getSpringData(url, RegisterDeviceResponse.class);
			return registerDeviceResponse;
		}
		public String getFacebookFeed(){
			return getFacebookFeed(25);
		}
		public String getFacebookFeed(int totalPosts){
			//String url = "https://graph.facebook.com/290805814352519/posts?access_token=CAACjs4PJeR8BAGk8o0PIYWC4DYEbrxM7eyB0BTabVEQd6z9KYESWPYiZCf6cw9AFM8NjdwMsEUfFWFuZBebZATqzkhXkDYt9zL1h7UoTP0oujFuaJ1u5oIFuFLSHc6GIZBccPgfAu9V8hsY3ZBIlDuMLjZAMI6Mo4DJiDPdyM6QUvYnoZANcmNVOfbz9zYHy1YSUvRY58qj7oioZApCHpsPx";
			/*
			
			String url = baseUrl + facebookFeedUrl;
			*/
			String token = "CAACjs4PJeR8BADXw3y37NEPwnyZBF94ga2OFLcnC5TuRZCbBv180ZAoCOWr4ZBwkgdDs5GQMmrC9PqSQKZAEhaMueoloZAXmN8Na2JeZCpvZC3juaSa4MSpW6ZBokkewdDzp2XUNBuBZBoqnIeN1kWv5WfGmsiEnZCOxlCBqjE111K4iZCHUkM3LXdKkXY21Xuef2RtYFMiFcJegrIXHZCPBPMa6y";
			if(Session.getActiveSession() != null && Session.getActiveSession().isOpened()){
				token = Session.getActiveSession().getAccessToken();
			}
			//String url = "https://graph.facebook.com/290805814352519/posts?limit="+totalPosts+"&fields=link,actions,id,name,picture,message,type,status_type&date_format=d-M-Y%20h:i%20e&access_token="+token;
			String url = "https://graph.facebook.com/290805814352519/posts?limit="+totalPosts+"&date_format=d-M-Y%20h:i%20e&access_token="+token;
			
			Log.i("DataServicesImpl","Hitting Get Url "+ url);
			
			RestTemplate restTemplate = new RestTemplate(true);
			//restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
			String responseNtity = restTemplate.getForObject(url, String.class);
			Log.i("DataServicesImpl","responseNtity.getBody : "+ responseNtity);
			/*
			HttpHeaders requestHeaders = new HttpHeaders();
			requestHeaders.setAcceptEncoding(ContentCodingType.GZIP);
			HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

			// Create a new RestTemplate instance
			RestTemplate restTemplate = new RestTemplate();

			// Add the String message converter
			restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

			// Make the HTTP GET request, marshaling the response to a String
			ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
			*/
			return responseNtity;
		}
		public String getSwarajBloggerFeed(){
			String url = "http://www.blogger.com/feeds/8832953156980338490/posts/default?alt=json&max-results=50&m=1";
			Log.i("DataServicesImpl","Hitting Get Url "+ url);
			RestTemplate restTemplate = new RestTemplate(true);
			//restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
			String responseNtity = restTemplate.getForObject(url, String.class);
			Log.i("DataServicesImpl","responseNtity.getBody : "+ responseNtity);
			return responseNtity;
		}
		public String getAapJanLokPalFeed(){
			String url = "http://www.blogger.com/feeds/2607599389053497858/posts/default?alt=json&max-results=50";
			Log.i("DataServicesImpl","Hitting Get Url "+ url);
			RestTemplate restTemplate = new RestTemplate(true);
			//restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
			String responseNtity = restTemplate.getForObject(url, String.class);
			Log.i("DataServicesImpl","responseNtity.getBody : "+ responseNtity);
			return responseNtity;
		}
		
		public RegisterFacebookUserResponse RegisterFacebookUserProfile(RegisterFacebookUserProfile registerFacebookUserProfile){
			String url = baseUrl + registerFacebookUserUrl;
			RegisterFacebookUserResponse registerFacebookUserResponse = postSpringData(url, RegisterFacebookUserResponse.class,registerFacebookUserProfile);
			return registerFacebookUserResponse;
		}
		
		public boolean likeFacebookPost(String postId,String accessToken){
			String url = "https://graph.facebook.com/"+postId+"/likes?access_token="+accessToken +"&method=POST";
			return getSpringData(url, boolean.class);
		}
		
		public String commentOnFacebookPost(String postId,String accessToken,String message){
			String url = "https://graph.facebook.com/"+postId+"/comments?access_token="+accessToken+"&method=POST&message="+message;
			
			Log.i("DataServicesImpl","Hitting Get Url "+ url);
			RestTemplate restTemplate = new RestTemplate(true);
			String response = restTemplate.getForObject(url, String.class);
			return response;
		}
		
		public DonationSummary getDonationSUmmary() {
			String url = baseUrl + donationSummaryUrl;
			DonationSummary donationSummary = getSpringData(url, DonationSummary.class);
			return donationSummary;
		}
		public List<StateDto> getAllStates() {
			String url = baseUrl + getAllStatesUrl;
			StateDto[] allStates = getSpringData(url, StateDto[].class);
			return Arrays.asList(allStates) ;
		}
		public List<DistrictDto> getDistrictsOfStates(long stateId) {
			String url = baseUrl + getDistrictOfStateUrl+"/"+stateId;
			DistrictDto[] allDistricts = getSpringData(url, DistrictDto[].class);
			return Arrays.asList(allDistricts) ;
		}
		public List<AssemblyConstituencyDto> getAssemblyConstituencyOfDistrict(long districtId) {
			String url = baseUrl + getAssemblyConstituencyOfDistrictUrl+"/"+districtId;
			AssemblyConstituencyDto[] allAssemblyConstituencyDtos = getSpringData(url, AssemblyConstituencyDto[].class);
			return Arrays.asList(allAssemblyConstituencyDtos) ;
		}
		public List<AssemblyConstituencyDto> getAssemblyConstituencyOfDelhi() {
			return getAssemblyConstituencyOfState(DataUtil.delhiStateId);
		}
		public List<AssemblyConstituencyWithCandidate> getAssemblyConstituencyOfDelhiForCandidate() {
			return getAssemblyConstituencyOfStateForCandidates(DataUtil.delhiStateId);
		}
		public List<AssemblyConstituencyWithManifesto> getAssemblyConstituencyOfDelhiForManifesto() {
			return getAssemblyConstituencyOfStateForManifesto(DataUtil.delhiStateId);
		}
		public List<AssemblyConstituencyDto> getAssemblyConstituencyOfState(long stateId) {
			String url = baseUrl + getAssemblyConstituencyOfStateUrl+"/"+stateId;
			AssemblyConstituencyDto[] allAssemblyConstituencyDtos = getSpringData(url, AssemblyConstituencyDto[].class);
			return Arrays.asList(allAssemblyConstituencyDtos) ;
		}
		public List<AssemblyConstituencyWithCandidate> getAssemblyConstituencyOfStateForCandidates(long stateId) {
			String url = baseUrl + getAssemblyConstituencyOfStateWithCandidatesUrl+"/"+stateId;
			AssemblyConstituencyWithCandidate[] allAssemblyConstituencyDtos = getSpringData(url, AssemblyConstituencyWithCandidate[].class);
			return Arrays.asList(allAssemblyConstituencyDtos) ;
		}
		public List<AssemblyConstituencyWithManifesto> getAssemblyConstituencyOfStateForManifesto(long stateId) {
			String url = baseUrl + getAssemblyConstituencyOfStateWithManifestoUrl+"/"+stateId;
			AssemblyConstituencyWithManifesto[] allAssemblyConstituencyDtos = getSpringData(url, AssemblyConstituencyWithManifesto[].class);
			return Arrays.asList(allAssemblyConstituencyDtos) ;
		}
		public ManifestoDto getAssemblyConstituencyManifesto(Context context,long acId) {
			String url = baseUrl + getAssemblyConstituencyManifestoUrl+"/"+ LanguageUtil.getLanguage(context) +"/" + acId;
			ManifestoDto manifestoDto = getSpringData(url, ManifestoDto.class);
			return manifestoDto;
		}
		public List<CandidateDto> getAssemblyConstituencyCandidates(Context context,long acId) {
			String url = baseUrl + getAssemblyConstituencyCandidatesUrl+"/"+ LanguageUtil.getLanguage(context) +"/" + acId;
			CandidateDto[] candidateDtos = getSpringData(url, CandidateDto[].class);
			return Arrays.asList(candidateDtos);
		}
				
		
}
